#!/bin/bash

DEST=`pwd`/build/libffmpeg && rm -rf $DEST
SOURCE=`pwd`/ffmpeg

export NDK=/home/dev/android/ndk-r10d

TOOLCHAIN=/home/dev/android/toolchain
#$NDK/build/tools/make-standalone-toolchain.sh --platform=android-21 \
 # --toolchain=arm-linux-androideabi-4.8 --arch=arm --install-dir=$TOOLCHAIN

SYSROOT=$TOOLCHAIN/sysroot/
export PATH=$TOOLCHAIN/bin:$PATH
export CC="ccache arm-linux-androideabi-gcc"
export LD=arm-linux-androideabi-ld
export AR=arm-linux-androideabi-ar

CFLAGS="-O3 -Wall -pipe -fpic -fasm \
  -finline-limit=300 -ffast-math \
  -fstrict-aliasing -Werror=strict-aliasing \
  -fmodulo-sched -fmodulo-sched-allow-regmoves \
  -Wno-psabi -Wa,--noexecstack \
  -DANDROID -DNDEBUG"

FFMPEG_FLAGS="--target-os=linux \
  --enable-cross-compile \
  --enable-asm \
  --enable-version3 \
  --enable-shared \
  --enable-small \
  --disable-static \
  --enable-memalign-hack \
  --enable-pthreads \
  --disable-symver \
  --disable-doc \
  --disable-programs \
  --disable-avdevice \
  --disable-devices \
  --disable-swscale \
  --disable-avfilter \
  --enable-swresample \
  --disable-postproc \
  --disable-network \
  --disable-dwt \
  --disable-faan \
  --disable-lsp \
  --disable-pixelutils \
  --disable-everything \
  --disable-dxva2 \
  --disable-vaapi \
  --disable-vda \
  --disable-vdpau \
  --disable-bsfs \
  --enable-protocol=file \
  --enable-decoder=pcm_s16le \
  --enable-decoder=flac \
  --enable-decoder=ape \
  --enable-decoder=tta \
  --enable-decoder=alac \
  --enable-decoder=aac \
  --enable-decoder=vorbis \
  --enable-decoder=mp3 \
  --enable-demuxer=wav \
  --enable-demuxer=flac \
  --enable-demuxer=ape \
  --enable-demuxer=tta \
  --enable-demuxer=mov \
  --enable-demuxer=aac \
  --enable-demuxer=ogg \
  --enable-demuxer=mp3 " 

#neon
for platform in armeabi armeabi-v7a; do

  cd $SOURCE

  case $platform in
    armeabi)
      STRIP=arm-linux-androideabi-strip
      ARCH="arm"
      CROSS_PREFIX="arm-linux-androideabi-"
      CFLAGS="$CFLAGS -mthumb -D__ARM_ARCH_5__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5TE__"
      EXTRA_CFLAGS="-march=armv6 -marm"
      EXTRA_LDFLAGS=""
      ;;
    armeabi-v7a)
      ARCH="arm"
      CROSS_PREFIX="arm-linux-androideabi-"
      STRIP=arm-linux-androideabi-strip
      CFLAGS="$CFLAGS -mthumb"
      EXTRA_CFLAGS="-march=armv7-a -marm -mfpu=vfpv3-d16 -mfloat-abi=softfp"
      EXTRA_LDFLAGS="-Wl,--fix-cortex-a8"
      ;;
    *)
      EXTRA_CFLAGS=""
      EXTRA_LDFLAGS=""
      ;;
  esac

  PREFIX="$DEST/$platform" && mkdir -p $PREFIX
  FFMPEG_FLAGS="$FFMPEG_FLAGS --arch=$ARCH --cross-prefix=$CROSS_PREFIX --prefix=$PREFIX"

  ./configure $FFMPEG_FLAGS --extra-cflags="$CFLAGS $EXTRA_CFLAGS" \
    --extra-ldflags="$EXTRA_LDFLAGS" | tee $PREFIX/configuration.txt

  sed -i 's/HAVE_LOG2 1/HAVE_LOG2 0/g' config.h
  sed -i 's/HAVE_LOG2F 1/HAVE_LOG2F 0/g' config.h
  sed -i 's/HAVE_LOG10F 1/HAVE_LOG10F 0/g' config.h

  cp config.* $PREFIX

  make clean
  make -j8 || exit 1
  make install || exit 1

  $CC -lm -lz -shared --sysroot=$SYSROOT -Wl,--no-undefined -Wl,-z,noexecstack \
    $EXTRA_LDFLAGS libavutil/*.o libavutil/$ARCH/*.o libavcodec/*.o libavcodec/$ARCH/*.o \
    libavformat/*.o libswresample/*.o libswresample/$ARCH/*.o -o $PREFIX/libffmpeg.so

  $STRIP --strip-unneeded $PREFIX/libffmpeg.so || exit 1

done

cd ..
zip -r libffmpeg.zip $DEST
mv libffmpeg.zip /data/
